<?php

class MasterPathController extends Zend_Controller_Action
{
	private $directoryUtil;
	
    public function init()
    {
        $ajaxContext = $this->_helper->getHelper('AjaxContext');
        $ajaxContext->addActionContext('foldersList', 'html')->initContext();
        
        $this->directoryUtil = new SubfolderAccessManager_Util_Directory();
    }

    public function indexAction()
    {
        $form = new Application_Form_ChangeMasterPath();

        $this->view->title = 'Master Path';
        $this->view->rootDir = $this->directoryUtil->getRootDirectory();
        $this->view->currentMasterPath = $this->directoryUtil->getMasterPath();
        $this->view->form = $form;
        $this->view->formResponse = '';

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();

            if (isset($_POST['changePath'])) {
	            if ($form->isValid($formData)) {
	                    $path = $form->getValue('path');
	                    $this->directoryUtil->setMasterPath($path);
	                    $this->_helper->redirector('index');
	            } else {
	                    $this->view->formResponse = 'Sorry, there was a problem with your submission. Please check the following:';
	                    $form->populate($formData);
	            }
            } else {
            	$this->directoryUtil->setMasterPath($this->view->rootDir);
            	$this->_helper->redirector('index');
            }
        }
    }

    public function foldersListAction()
    {
        $this->view->directory = $this->getRequest()->getParam('dir');
        $this->view->folderExceptions = $this->directoryUtil->getFolderExceptions();

        $this->_helper->layout()->disableLayout();
    }
}